package com.tanhua.server;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.Question;
import com.tanhua.model.Settings;
import com.tanhua.model.UserInfo;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private BlackListApi blackListApi;
    /*
    * 查询通用设置
    * */
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //获取用户的id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //获取用户的手机号码
        String mobile = UserHolder.getMobile();
        System.out.println(mobile);
        vo.setPhone(UserHolder.getMobile());
        //获取用户设置的陌生问题
        Question question = questionApi.findByUserId(userId);
        String txt = question ==null ? "想要去看看星星吗？": question.getTxt();
        vo.setStrangerQuestion(txt);
        //获取用户设置的通用设置
        Settings settings = settingsApi.findByUserId(userId);
        if(settings!=null){
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return  vo;
    }
    /*
    * 设置问题
    * */
    public void saveQuestion(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        //判断question是否为null，如果为null表示以前没有设置过,需要进行添加
        if(question==null){
             question = new Question();
             question.setUserId(userId);
             question.setTxt(content);
             questionApi.save(question);
        }else{
            //不为null表示设置过，需要进行修改
            question.setTxt(content);
            questionApi.update(question);
        }
    }
    /*
    * 通知设置
    * */
    public void saveNotifications(Map map) {
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);
        if(settings ==null){
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification((boolean)map.get("gonggaoNotification"));
            settings.setLikeNotification((boolean)map.get("likeNotification"));
            settings.setPinglunNotification((boolean)map.get("pinglunNotification"));
            settingsApi.save(settings);
        }else {
            settings.setGonggaoNotification((boolean)map.get("gonggaoNotification"));
            settings.setLikeNotification((boolean)map.get("likeNotification"));
            settings.setPinglunNotification((boolean)map.get("pinglunNotification"));
            settingsApi.update(settings);
        }
    }
    /*
    * 黑名单列表查询
    * */
    public PageResult blacklist(int page, int size) {
        Long userId = UserHolder.getUserId();
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, size);
        PageResult pageResult = new PageResult(page, size, iPage.getTotal(), iPage.getRecords());
        return pageResult;
      }
    /*
    * 删除好友
    * */
    public void blacklistUid(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.delete(userId,blackUserId);
    }
}
